package hlhj.fhp.supreme.utils;

import android.util.Log;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import fhp.hlhj.giantfold.utils.LogUtil;

public class OrderInfoUtil2_0 {
	
	/**
	 * 构造授权参数列表
	 * 
	 * @param pid
	 * @param app_id
	 * @param target_id
	 * @return
	 */
	public static Map<String, String> buildAuthInfoMap(String pid, String app_id, String target_id, boolean rsa2) {
		Map<String, String> keyValues = new HashMap<String, String>();

		// 商户签约拿到的app_id，如：2013081700024223
		keyValues.put("app_id", app_id);

		// 商户签约拿到的pid，如：2088102123816631
		keyValues.put("pid", pid);

		// 服务接口名称， 固定值
		keyValues.put("apiname", "com.alipay.account.auth");

		// 商户类型标识， 固定值
		keyValues.put("app_name", "mc");

		// 业务类型， 固定值
		keyValues.put("biz_type", "openservice");

		// 产品码， 固定值
		keyValues.put("product_id", "APP_FAST_LOGIN");

		// 授权范围， 固定值
		keyValues.put("scope", "kuaijie");

		// 商户唯一标识，如：kkkkk091125
		keyValues.put("target_id", target_id);

		// 授权类型， 固定值
		keyValues.put("auth_type", "AUTHACCOUNT");

		// 签名类型
		keyValues.put("sign_type", rsa2 ? "RSA2" : "RSA");

		return keyValues;
	}

	/**
	 * 构造支付订单参数列表
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public static Map<String,String> buildOrderParamMap(String appid,String charset,String method,String signtype,String version,String timestamp, String bizcontent,String notify_url) {
		Map<String, String> keyValues = new TreeMap<>();
		keyValues.put("app_id",appid);
		keyValues.put("biz_content",bizcontent);
		keyValues.put("charset", charset);
		keyValues.put("format", "JSON");
		keyValues.put("method", method);
		keyValues.put("sign_type",signtype);
		keyValues.put("timestamp", timestamp);
		keyValues.put("version", version);
		//keyValues.put("notify_url", "http://userapi.nongyinong.cn/pay/alipay_notify");// 正式
		keyValues.put("notify_url", notify_url);// 测试

		return keyValues;
	}

	/**
	 * 构造业务参数
	 * @param body
	 * @param subject
	 * @param trade
     * @return
     */
	public static String builBizcontent(String body, String subject, String trade, String total_amount,String timeout_express){
		Map<String,String> map = new TreeMap<>();
//		map.put("body",body);
		map.put("subject",subject);
		map.put("out_trade_no",trade);
		map.put("total_amount",total_amount);
		map.put("timeout_express",timeout_express);
//		map.put("seller_id","jiangguoshuaia@163.com");
		map.put("product_code","QUICK_MSECURITY_PAY");
		map.put("goods_type","1");
		JSONObject obj = new JSONObject(map);
		return obj.toString();
	}
	/**
	 * 构造支付订单参数信息
	 * 
	 * @param map
	 * 支付订单参数
	 * @return
	 */
    public static String buildOrderParam(Map<String, String> map) {
        List<String> keys = new ArrayList<String>(map.keySet());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keys.size() - 1; i++) {
            String key = keys.get(i);
            String value = map.get(key);
            sb.append(buildKeyValue(key, value, true));
            sb.append("&");
        }

        String tailKey = keys.get(keys.size() - 1);
        String tailValue = map.get(tailKey);
        sb.append(buildKeyValue(tailKey, tailValue, true));

        return sb.toString();
    }
	/**
	 * 拼接键值对
	 * 
	 * @param key
	 * @param value
	 * @param isEncode
	 * @return
	 */
	private static String buildKeyValue(String key, String value, boolean isEncode) {
		StringBuilder sb = new StringBuilder();
		sb.append(key);
		sb.append("=");
//		sb.append(value);
		if (isEncode) {
			try {
				sb.append(URLEncoder.encode(value, "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				sb.append(value);
			}
		} else {
			sb.append(value);
		}
		return sb.toString();
	}
	
	/**
	 * 对支付参数信息进行签名
	 * 
	 * @param map
	 *            待签名授权信息
	 * 
	 * @return
	 */
	public static String getSign(Map<String, String> map, String rsaKey, boolean rsa2) {
        List<String> keys = new ArrayList<String>(map.keySet());
        // key排序
        Collections.sort(keys);

        StringBuilder authInfo = new StringBuilder();
        for (int i = 0; i < keys.size() - 1; i++) {
            String key = keys.get(i);
            String value = map.get(key);
            authInfo.append(buildKeyValue(key, value, false));
            authInfo.append("&");
        }

        String tailKey = keys.get(keys.size() - 1);
        String tailValue = map.get(tailKey);
        authInfo.append(buildKeyValue(tailKey, tailValue, false));

        String oriSign = SignUtils.sign(authInfo.toString(), rsaKey, rsa2);
        String encodedSign = "";

        try {
            encodedSign = URLEncoder.encode(oriSign, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "sign=" + encodedSign;
	}
	
	/**
	 * 要求外部订单号必须唯一。
	 * @return
	 */
	private static String getOutTradeNo() {
		SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
		Date date = new Date();
		String key = format.format(date);

		Random r = new Random();
		key = key + r.nextInt();
		key = key.substring(0, 15);
		return key;
	}
	public static String toURLEncoded(String paramString) {
		if (paramString == null || paramString.equals("")) {
			LogUtil.INSTANCE.logi("toURLEncoded error:"+paramString);
			return "";
		}

		try
		{
			String str = new String(paramString.getBytes(), "UTF-8");
			str = URLEncoder.encode(str, "UTF-8");
			return str;
		}
		catch (Exception localException)
		{
			LogUtil.INSTANCE.logi(localException.toString());
//			LogUtil.INSTANCE.log("toURLEncoded error:"+paramString, localException);
		}

		return "";
	}
}
